﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NGE;

namespace MH.SE
{
    public partial class MonsterEditer : UserControl
    {
        Game game;
        List<Character> mons;
        Rectangle zone;
        MonsterCode mc=new MonsterCode();
        public Rectangle Zone
        {
            set
            {
                zone = value;
                textBox4.Text = "" + value.X;
                textBox5.Text = "" + value.Y;
                textBox3.Text = "" + value.Width;
                textBox2.Text = "" + value.Height;
            }
        }
        public void refresh()
        {
            if (((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer == null)
            {
                return;
            }
            comboBox2.Items.Clear();
            for (int i = 0; i < ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer.Count; i++)
            {
                comboBox2.Items.Add(((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Zone.Location.ToString());
            }
        }
        public void LoadScene(Game g)
        {
            game = g;
        }
        public MonsterEditer()
        {
            InitializeComponent();
            
            if (File.Exists("data\\character\\mons.nchrs"))
            {
                mons = (List<Character>)Util.LoadObject("data\\character\\mons.nchrs");
            }
            else
            {
                mons = new List<Character>();
            }

            comboBox3.Items.Clear();
            for (int i = 0; i < mons.Count; i++)
            {
                comboBox3.Items.Add(mons[i].Name);
            }
        }

        private void MonsterEditer_Load(object sender, EventArgs e)
        {
            mc = new MonsterCode();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Add(comboBox3.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0)
            {
                return;
            }
            comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mc=new MonsterCode();
            mc.BattleRate=int.Parse(textBox1.Text);
            mc.BattleMusic = textBox6.Text;
            mc.Zone = new Rectangle(int.Parse(textBox4.Text),int.Parse(textBox5.Text),int.Parse(textBox3.Text),int.Parse(textBox2.Text));
            for(int i=0;i<comboBox1.Items.Count;i++)
            {
                mc.MonsterNames.Add(comboBox1.Items[i].ToString());
            }
            for (int i = 0; i < textBox9.Lines.Length; i++)
            {
                string str = textBox9.Lines[i];
                string[] str2 = str.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                Spoil spoil = new Spoil();
                spoil.Name = str2[0];
                spoil.Seed = int.Parse(str2[1]);
                mc.Spoils.Add(spoil);
            }
            if (((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer == null)
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer = new MonsterCodeLayer();
            }
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer.Add(mc);
            refresh();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = comboBox2.SelectedIndex;
            textBox4.Text =""+((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Zone.X;
            textBox5.Text =""+((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Zone.Y;
            textBox3.Text =""+((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Zone.Width;
            textBox2.Text =""+((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Zone.Height;
            textBox1.Text = "" + ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].BattleRate;
            textBox6.Text = "" + ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].BattleMusic;
            comboBox1.Items.Clear();
            for (int j = 0; j < ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].MonsterNames.Count; j++)
            {
                comboBox1.Items.Add(((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].MonsterNames[j]);
            }
            textBox9.Text = "";
            for (int j = 0; j < ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Spoils.Count; j++)
            {
                textBox9.Text += ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Spoils[j].Name + "=" + ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer[i].Spoils[j].Seed + "\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MonsterCodeLayer.MonsterCodeSet.RemoveAt(comboBox2.SelectedIndex); refresh();
        }
    }
}
